﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WF_QLThuVien.Entities.PhieuMuon;

namespace WF_QLThuVien.DataLayer.PhieuMuon
{
    class CDAL_CTPhieuMuon
    {
        DataAccessHelper db = new DataAccessHelper();

        public bool Insert(CCTPhieuMuon ctpm)
        {
            try
            {
                int count = db.ExecuteNonQuery("INSERT INTO ChiTietPhieuMuon VALUES('" + ctpm.PhieuMuonID + "','" + ctpm.Sach.SachID + "'," + ctpm.TrangThai + ",#" + ctpm.NgayTra + "#," + ctpm.SoLanGiaHan + ")");
                return count > 0;
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi CDAL_CTPhieuMuon.Insert \n" + ex.ToString());
            }
        }

        public bool Update(CCTPhieuMuon ctpm)
        {
            try
            {
                int count = db.ExecuteNonQuery("UPDATE ChiTietPhieuMuon SET"
                    + " SachID = '" + ctpm.Sach.SachID + "'"
                    + ",TrangThai = " + ctpm.TrangThai + ""
                    + ",SoLanGiaHan=" + ctpm.SoLanGiaHan + ""
                        + " WHERE PhieuMuonID = " + ctpm.PhieuMuonID);
                return count > 0;
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi CDAL_CTPhieuMuon.Update \n" + ex.ToString());
            }
        }

        public CCTPhieuMuon GetCTPMByDocGiaIDSachID(string docgiaID, string sachID)
        {
            try
            {
                CCTPhieuMuon ctpm = new CCTPhieuMuon();
                string cmd = "SELECT * FROM ChiTietPhieuMuon ctpm, PhieuMuon pm WHERE pm.PhieuMuonID=ctpm.PhieuMuonID and SachID='" + sachID + "' and DocGiaID='" + docgiaID + "'";
                //db.get
                return ctpm;
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi CDAL_CTPhieuMuon.GetCTPMByDocGiaIDSachID \n" + ex.ToString());
            }

        }
    }
}
